//
//  CustomTabItemView.m
//  baojia
//
//  Created by Zing on 14-8-5.
//  Copyright (c) 2014年 com.baojia.www. All rights reserved.
//

#import "CustomTabItemView.h"
#import "Button.h"

@interface CustomTabItemView()

@property(nonatomic , strong) UIImageView * sliderBar;
@property(nonatomic , strong) UIButton * lastButton;
@end

@implementation CustomTabItemView

- (id)initWithFrame:(CGRect)frame type:(TAB_ITEM_TYPE)type
{
    self = [super initWithFrame:frame];
    if (self) {
        self.tType = type;
        self.backgroundColor = [UIColor clearColor];
        
    }
    return self;
}

- (void)tabItemSelectedAtIndexBlock:(void(^)(int index,CustomTabItemView * view)) block
{
    self.tabItemSelectedAtIndexBlock = [block copy];
}

- (void)initWithItemNames:(NSArray *)itemNames
{
    UIImageView * line = [[UIImageView alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(self.frame) - 3, CGRectGetWidth(self.frame), 1)];
    line.backgroundColor = COLOR_RGBA(220, 220, 220, 1);
    [self addSubview:line];
    
    int count = [itemNames count];
    float width = CGRectGetWidth(self.frame) / count;
    float height = CGRectGetHeight(self.frame);
    
    for (int i = 0; i < count; i ++) {
        UIButton * btn = [[UIButton alloc] initWithFrame:CGRectMake(width * i, 0, width, height - 3)];
        [btn setTitle:[itemNames objectAtIndex:i] forState:UIControlStateNormal];
        btn.tag = 1000 + i;
        [btn setTitleColor:MAIN_BLACK_COLOR forState:UIControlStateNormal];
        [btn.titleLabel setFont:[UIFont boldSystemFontOfSize:14]];
        [btn setTitleColor:MAIN_BLUE_COLOR forState:UIControlStateSelected];
        [btn setBackgroundColor:COLOR_RGBA(245, 245, 245, 1)];
        [btn addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn];
        if (i == 0) {
            _lastButton = btn;
            btn.selected = YES;
            [self initSliderBar:btn.frame];
        }
    }
}


- (void)initWithBackColorCornerItemNames:(NSArray *)itemNames
{
    self.frame = CGRectMake(5, (self.height - 30)/2, CGRectGetWidth(self.frame)-10, 30);
    self.layer.borderColor = MAIN_BLUE_COLOR.CGColor;
    self.layer.borderWidth  = 1;
    [self.layer setCornerRadius:6];
    self.clipsToBounds = YES;
    
    int count = [itemNames count];
    float width = CGRectGetWidth(self.frame) / count;
    float height = CGRectGetHeight(self.frame);
    
    for (int i = 0; i < count; i ++) {
        UIButton * btn = [[UIButton alloc] initWithFrame:CGRectMake(width * i, 0, width, height)];
        [btn setTitle:[itemNames objectAtIndex:i] forState:UIControlStateNormal];
        btn.tag = 1000 + i;
        [btn setTitleColor:MAIN_BLUE_COLOR forState:UIControlStateNormal];
        [btn.titleLabel setFont:[UIFont boldSystemFontOfSize:14]];
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        UIImage* colorImg = [UIImage createImageWithBgColor:MAIN_BLUE_COLOR size:CGSizeMake(btn.width, btn.height-5) cornerRadius:0];
        [btn setBackgroundImage:colorImg forState:UIControlStateSelected];
        [btn addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn];
        if (i == 0) {
            _lastButton = btn;
            btn.selected = YES;
        }
        
        if (i < count - 1) {
            UIView *seperate = [[UIView alloc]initWithFrame:CGRectMake(btn.right, btn.top, 1, btn.height)];
            seperate.backgroundColor = MAIN_BLUE_COLOR;
            [self addSubview:seperate];
        }
    }
}


- (void)initWithLineItemNames:(NSArray *)itemNames

{
    NSArray *array;
    if ([self.orderModel isEqual:@""] || self.orderModel == nil) {
        array = @[@"0",@"0",@"0",@"0"];
    }else
    {
        array = @[self.orderModel.news,self.orderModel.paids,self.orderModel.deliverds,self.orderModel.finisheds];
    }
  
    UIImageView * line = [[UIImageView alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(self.frame) - 3, CGRectGetWidth(self.frame), 1)];
    line.backgroundColor = COLOR_RGBA(220, 220, 220, 1);
    [self addSubview:line];
    
    int count = [itemNames count];
    float width = CGRectGetWidth(self.frame) / count;
    float height = CGRectGetHeight(self.frame);
    
    for (int i = 0; i < count; i ++) {
        _btn = [[UIButton alloc] initWithFrame:CGRectMake(width * i + i * 1 , 0, width, height - 3)];
        [_btn setTitle:[itemNames objectAtIndex:i] forState:UIControlStateNormal];
        _btn.tag = 1000 + i;
        [_btn setTitleColor:MAIN_BLACK_COLOR forState:UIControlStateNormal];
        [_btn.titleLabel setFont:[UIFont boldSystemFontOfSize:14]];
        [_btn setTitleColor:MAIN_BLACK_COLOR forState:UIControlStateHighlighted];
        [_btn setBackgroundColor:COLOR_RGBA(245, 245, 245, 1)];
        [_btn addTarget:self action:@selector(btnChoosePressed:) forControlEvents:UIControlEventTouchUpInside];
        
        int y;
        if (kHeight>=480) {
            y = 63;
        }else{
            y = 60;
        }
        UIImageView  *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(y, 0, 18, 18)];
        imageView.bottom = _btn.top + 20;
        imageView.tag = 10000 + i;
        imageView.layer.cornerRadius = 8;
        [imageView setBackgroundColor:[UIColor redColor]];
        
        
        _budgeNumLabel = [[UILabel alloc]initWithFrame:CGRectMake((18-10)/2, (18-10)/2, 10, 10)];
        _budgeNumLabel.font = [UIFont systemFontOfSize:11.0f];
        _budgeNumLabel.tag = 10 + i;
        _budgeNumLabel.textAlignment = NSTextAlignmentLeft;
        _budgeNumLabel.textColor = [UIColor whiteColor];
        [imageView addSubview:_budgeNumLabel];
        
         _budgeNumLabel.text = array[i];
        
        
//        if (![array[i]  isEqual: @"(null)"] || array[i] != 0) {
//
//            [_btn addSubview:imageView];
//            
//        }
        if ([array[i] isEqualToString:@"(null)"]) {
            _budgeNumLabel.text = @"0";
        }else{
            _budgeNumLabel.text = array[i];
        }
        [_btn addSubview:imageView];
        
        [self addSubview:_btn];
        
        if (i < count - 1) {
            UIView *seperate = [[UIView alloc]initWithFrame:CGRectMake(_btn.right, _btn.top + 5, 1, _btn.height -10)];
            seperate.backgroundColor = MAIN_GRAY_COLOR;
            [self addSubview:seperate];
        }
        if (i == 0) {
            _lastButton = _btn;
            _btn.selected = YES;
            [self initSliderBar:_btn.frame];
        }
    }
}

- (void)initWithLineItemName:(NSArray *)itemNames

{
    UIImageView * line = [[UIImageView alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(self.frame) - 3, CGRectGetWidth(self.frame), 1)];
    line.backgroundColor = COLOR_RGBA(220, 220, 220, 1);
    [self addSubview:line];
    
    int count = [itemNames count];
    float width = CGRectGetWidth(self.frame) / count;
    float height = CGRectGetHeight(self.frame);
    
    for (int i = 0; i < count; i ++) {
        _btn = [[UIButton alloc] initWithFrame:CGRectMake(width * i + i * 1 , 0, width, height - 3)];
        [_btn setTitle:[itemNames objectAtIndex:i] forState:UIControlStateNormal];
        _btn.tag = 1000 + i;
        [_btn setTitleColor:MAIN_BLACK_COLOR forState:UIControlStateNormal];
        [_btn.titleLabel setFont:[UIFont boldSystemFontOfSize:14]];
        [_btn setTitleColor:MAIN_BLACK_COLOR forState:UIControlStateHighlighted];
        [_btn setBackgroundColor:COLOR_RGBA(245, 245, 245, 1)];
        [_btn addTarget:self action:@selector(btnChoosePressed:) forControlEvents:UIControlEventTouchUpInside];
        
        
        UIImageView  *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(60, 0, 18, 18)];
        imageView.bottom = _btn.top + 20;
        imageView.tag = 10000 + i;
        imageView.layer.cornerRadius = 8;
        [imageView setBackgroundColor:[UIColor redColor]];
        
        [self addSubview:_btn];
        
        if (i < count - 1) {
            UIView *seperate = [[UIView alloc]initWithFrame:CGRectMake(_btn.right, _btn.top + 5, 1, _btn.height -10)];
            seperate.backgroundColor = MAIN_GRAY_COLOR;
            [self addSubview:seperate];
        }
        if (i == 0) {
            _lastButton = _btn;
            _btn.selected = YES;
            [self initSliderBar:_btn.frame];
        }
    }
}


- (void)initWithNearbyItemNames:(NSArray *)itemNames
{
    UIImageView * line = [[UIImageView alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(self.frame) - 3, CGRectGetWidth(self.frame), 1)];
    line.backgroundColor = COLOR_RGBA(220, 220, 220, 1);
    [self addSubview:line];
    
    self.backgroundColor = [UIColor whiteColor];
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kWidth, 40)];
    [self addSubview:view];
    
    
   
    CGFloat width = (self.width - (itemNames.count - 1) * 1 - itemNames.count * 10) / itemNames.count;
    
    for (int index = 0; index < itemNames.count; index++ ) {
        
        NSString *title = [itemNames objectAtIndex:index];
        
        Button  *button = [Button buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(index * 5 + (index + 1) * 5 + width * index, 5, width, 30);
        button.tag = 1000+index;
        
        [button setTitleColor:MAIN_BLACK_COLOR forState:UIControlStateNormal];
        [button.titleLabel setFont:[UIFont boldSystemFontOfSize:14]];
        [button setTitleColor:MAIN_BLUE_COLOR forState:UIControlStateSelected];
        [button setTitle:title forState:UIControlStateNormal];
        [button addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];
        [view addSubview:button];
        
        if (index == 0) {
            _lastButton = button;
            button.selected = YES;
            [self initSliderBar:button.frame];
        }
        
        if (index < itemNames.count - 1) {
            UIView *line = [[UIView alloc]init];
            line.frame = CGRectMake(button.right + 5, 5, 1, 30);
            line.backgroundColor = [UIColor lightGrayColor];
            [view addSubview:line];
        }
        
    }
    
    UIView *separateLine = [[UIView alloc]init];
    separateLine.frame = CGRectMake(0, view.height - 0.5, kWidth, 0.5);
    separateLine.backgroundColor = [UIColor lightGrayColor];
    [self addSubview:separateLine];
    
    UIView *separateView = [[UIView alloc]init];
    separateView.frame = CGRectMake(0,separateLine.bottom, kWidth, 10);
    separateView.backgroundColor = UIColorFromRGB(0xdcdddd);
    [self addSubview:separateView];
    
    UIView *separateLine1 = [[UIView alloc]init];
    separateLine1.frame = CGRectMake(0, separateView.height -3, kWidth, 1.5);
    separateLine1.backgroundColor = [UIColor whiteColor];
    [separateView addSubview:separateLine1];
    
    
}


- (void)initWithChooseItemNames:(NSArray *)itemNames{
    
    self.backgroundColor = [UIColor clearColor];
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kWidth, 40)];
    [self addSubview:view];
    
    
    UIImage *image = [UIImage imageNamed:@"arrows"];
    CGFloat width = (self.width - (itemNames.count - 1) * 1 - itemNames.count * 10) / itemNames.count;
    
    for (int index = 0; index < itemNames.count; index++ ) {
        
        NSString *title = [itemNames objectAtIndex:index];
        
        Button  *button = [Button buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(index * 5 + (index + 1) * 5 + width * index, 0, width, 30);
        button.tag = 1000+index;
        
        [button setTitleColor:MAIN_BLACK_COLOR forState:UIControlStateNormal];
        [button.titleLabel setFont:[UIFont boldSystemFontOfSize:14]];
        [button setTitleColor:UIColorFromRGB(0X9fa0a0) forState:UIControlStateNormal];
        [button setTitle:title forState:UIControlStateNormal];
        [button setImage:image forState:UIControlStateNormal];
        [button addTarget:self action:@selector(btnChoosePressed:) forControlEvents:UIControlEventTouchUpInside];
        [view addSubview:button];
        
        
        if (index < itemNames.count - 1) {
            UIView *line = [[UIView alloc]init];
            line.frame = CGRectMake(button.right + 5, 5, 1, 20);
            line.backgroundColor = [UIColor lightGrayColor];
            [view addSubview:line];
        }
    }
    
}

- (void)setItemEnableStatusWithIndex:(int)index status:(BOOL)status
{
    UIButton * sender = (UIButton *)[self viewWithTag:1000 + index];
    sender.userInteractionEnabled = status;
    if (status) {
        [sender setTitleColor:MAIN_BLACK_COLOR forState:UIControlStateNormal];
    }else{
        [sender setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    }
    
}

- (void)setSelectedIndex:(int)index
{
    UIButton * sender = (UIButton *)[self viewWithTag:1000 + index];
    [self initSliderBar:sender.frame];
    [self btnPressed:sender];
}

- (void)initSliderBar:(CGRect)btnFrame
{
    if (!self.sliderBar) {
        self.sliderBar = [UIImageView new];
        self.sliderBar.backgroundColor = MAIN_BLUE_COLOR;
        [self addSubview:self.sliderBar];
    }
    [UIView animateWithDuration:0.1f animations:^{
        CGRect frame = self.sliderBar.frame;
        frame.origin.y = CGRectGetHeight(self.frame) - 2;
        frame.size.height = 2;
        frame.size.width = (btnFrame.size.width - 20);
        frame.origin.x = (btnFrame.origin.x + 10);
        self.sliderBar.frame = frame;
    }];
}

- (void)btnPressed:(UIButton *)sender
{
    int index = sender.tag - 1000;
    if (_lastButton != sender)
    {
        //取消上次按钮的选中效果
        _lastButton.selected = NO;
    }
    _lastButton = sender;
    
    //选中当前点击按钮
    sender.selected = YES;
    if(_tType == TAB_ITEM_TYPE_CORWIDTH){
        
    }else{
        [sender setTitleColor:MAIN_BLUE_COLOR forState:UIControlStateSelected];
        [self initSliderBar:sender.frame];
    }
    
    if ([self.delegate respondsToSelector:@selector(tabItemSelectedAtIndex:tabItem:)]) {
        [self.delegate tabItemSelectedAtIndex:index tabItem:self];
    }
    
    if (self.tabItemSelectedAtIndexBlock) {
        self.tabItemSelectedAtIndexBlock(index,self);
    }
    
}

-(void)btnChoosePressed:(UIButton *)sender
{
    int index = sender.tag - 1000;
    if (_lastButton != sender)
    {
        //取消上次按钮的选中效果
        _lastButton.selected = NO;
    }
    _lastButton = sender;
    
    //选中当前点击按钮
    sender.selected = YES;
    if(_tType == TAB_ITEM_TYPE_CORWIDTH){
        
    }else{
        [sender setTitleColor:MAIN_BLUE_COLOR forState:UIControlStateSelected];
        [self initSliderBar:sender.frame];
    }
    
    if ([self.delegate respondsToSelector:@selector(tabItemSelectedAtIndex:tabButton:tabItem:)]) {
        [self.delegate tabItemSelectedAtIndex:index tabButton:sender tabItem:self];
    }
    
    if (self.tabItemSelectedAtIndexBlock) {
        self.tabItemSelectedAtIndexBlock(index,self);
    }
    
}

-(void)refreshBugeNum:(NSString *)bugedNum indexButton:(int)index{
    
    UILabel *btnLabel = (UILabel *)[self viewWithTag:index];
    btnLabel.text = bugedNum;
}


/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect
 {
 // Drawing code
 }
 */

@end
